package com.lzb.compressmaster.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.Content;
import com.lzb.compressmaster.HomeActivity;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.databinding.ActivityFileDecompressBinding;
import com.lzb.compressmaster.dialog.CompressDialog;
import com.lzb.compressmaster.utils.ApacheCompressUtil;
import com.lzb.compressmaster.utils.BatchDecompressUtil;

import java.util.List;

public class FileDecompressActivity extends BaseAc<ActivityFileDecompressBinding> implements View.OnClickListener {

    public static List<String> listPath;
    private boolean isDelete;

    @Override
    protected int onCreate() {
        return R.layout.activity_file_decompress;
    }

    @Override
    protected void initView() {
        mDataBinding.ivFileDecompressBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llNoDelete.setOnClickListener(this);
        mDataBinding.llDelete.setOnClickListener(this);
        mDataBinding.tvStartDecompress.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.llNoDelete) {//不删除
            clearSelection();
            isDelete = false;
            mDataBinding.ivNoDelete.setImageResource(R.drawable.axz2);
        } else if (id == R.id.llDelete) {//删除
            clearSelection();
            isDelete = true;
            mDataBinding.ivDelete.setImageResource(R.drawable.axz2);
        } else if (id == R.id.tvStartDecompress) {//开始解压
            showCompressDialog();
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvFileSize.setText(listPath.size() + getString(R.string.ge_unit));
    }

    private void startDecompress() {
        String path = PathUtils.getExternalDocumentsPath() + Content.myDecompressFile;
        BatchDecompressUtil.batchDecompress(mContext, listPath, path, new BatchDecompressUtil.DecompressCallback() {
            @Override
            public void onProgress(int current, int total, String currentFile) {
                showDialog(getString(R.string.decompress_ing));
            }

            @Override
            public void onSuccess(List<String> extractedFiles) {
                dismissDialog();
                if (isDelete) {
                    for (String path : listPath) {
                        FileUtils.delete(path);
                    }
                }
                ToastUtils.showShort(getString(R.string.decompress_suc));
                finish();
            }

            @Override
            public void onError(String error) {
                dismissDialog();
                ToastUtils.showShort(error);
            }
        });
    }

    private void showCompressDialog() {
        CompressDialog compressDialog = new CompressDialog(mContext);
        compressDialog.setCompress(false);
        compressDialog.setListener(new CompressDialog.Ilistener() {
            @Override
            public void clickConfirm() {
                startDecompress();
            }
        });
        compressDialog.show();
    }

    private void clearSelection() {
        mDataBinding.ivNoDelete.setImageResource(R.drawable.awxz);
        mDataBinding.ivDelete.setImageResource(R.drawable.awxz);
    }

}